<style lang="less">
</style>
<template>
    <div class="search">
        <Row>
            <Col>
                <Card>
                    <Row>
                        <Form ref="searchForm" :model="searchForm" inline :label-width="160" class="search-form">
                            <Row class="operation">
                                <i-col span="8">
                                    <Form-item label="渠道归属" prop="channelBelong">
                                        <Select v-model="searchForm.channelBelong" placeholder="请选择" clearable style="width: 200px">
                                            <Option :value="item.value" v-for="(item,index) in channelBelongList" :key="index"> {{item.title}}</Option>
                                        </Select>
                                    </Form-item>
                                </i-col>

                                <i-col span="8">
                                    <Form-item label="是否紧急" prop="isUrgent">
                                        <Select v-model="searchForm.isUrgent" placeholder="请选择" clearable style="width: 200px">
                                            <Option :value="item.value" v-for="(item,index) in isUrgentList" :key="index"> {{item.title}}</Option>
                                        </Select>
                                    </Form-item>
                                </i-col>
                            </Row>

                            <Row  style="text-align: center">
                                <Form-item style="margin-left:-60px; text-align: center" class="br">
                                    <Button @click="handleSearch" type="primary" icon="ios-search">查询</Button>
                                    <Button @click="handleReset" icon="md-redo">重置</Button>
                                </Form-item>
                            </Row>
                        </Form>
                    </Row>
                    <Row class="operation">
                        <Button @click="add" type="primary" icon="md-add">新增</Button>
                        <Button @click="delAll" icon="md-trash">批量删除</Button>
                        <circleLoading v-if="operationLoading"/>
                    </Row>
                    <Row>
                        <Alert show-icon>
                            已选择 <span class="select-count">{{selectCount}}</span> 项
                            <a class="select-clear" @click="clearSelectAll">清空</a>
                        </Alert>
                    </Row>
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" sortable="custom"
                               @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table">
                            <template slot-scope="{ row, index }" slot="action">
                                <Button style="margin-right:5px" type="primary" size="small" @click="edit(row)">编辑</Button>
                            </template>
                            <template slot-scope="{ row, index }" slot="channelBelong">
                                <span>{{setChannelBelongTitle(row.channelBelong)}}</span>
                            </template>
                            <template slot-scope="{ row, index }" slot="isUrgent">
                                <span>{{setIsUrgentListTitle(row.isUrgent)}}</span>
                            </template>
                        </Table>
                    </Row>

                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize"
                              @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]"
                              size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>
        <Modal :title="modalTitle" v-model="announcementInfoModalVisible" :mask-closable='false' :width="1000"
               :styles="{top: '100px'}">
            <Form ref="announcementInfoForm" :model="announcementInfoForm" :label-width="90" :rules="announcementInfoFormValidate">
                <Row>
                    <Col span="8">
                        <FormItem label="标题" prop="title">
                            <Input v-model="announcementInfoForm.title"   class="w200"  placeholder="请输入"/>
                        </FormItem>
                    </Col>
                    <Col span="8">
                        <FormItem label="是否紧急" prop="isUrgent">
                            <Select v-model="announcementInfoForm.isUrgent" placeholder="请选择" clearable style="width: 200px">
                                <Option :value="item.value" v-for="(item,index) in isUrgentList" :key="index"> {{item.title}}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                    <Col span="8">
                        <FormItem label="渠道归属" prop="channelBelong">
                            <Select v-model="announcementInfoForm.channelBelong" placeholder="请选择" clearable style="width: 200px">
                                <Option :value="item.value" v-for="(item,index) in channelBelongList" :key="index"> {{item.title}}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span="18">
                        <FormItem label="内容" prop="content" >
                            <Input v-model="announcementInfoForm.content" clearable  class="w400"  maxlength="300" placeholder="请输入" type="textarea" :rows="6"/>
                        </FormItem>
                    </Col>
                </Row>
            </Form>
            <div slot="footer">
                <Button type="text" @click="cancel">取消</Button>
                <Button type="primary" :loading="saveLoading" @click="save">保存</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import {
        getAnnouncementInfo,
        saveAnnouncementInfo,
        delAnnouncementInfo
    } from "@/projects/afs-apply/api/app/app";
    import {getDictDataByType} from "_p/basic/api/admin/datadic";

    export default {
        name: "announcementInfo",
        components: {
        },
        data() {
            return {
                loading: true,
                drop: false,
                selectCount: 0,
                selectList: [],
                operationLoading: false,
                modalTitle: "",
                announcementInfoModalVisible:false,
                saveLoading:false,
                searchForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    operatingSystem:""
                },
                isUrgentList:[],
                channelBelongList:[],
                announcementInfoForm :{
                    title:"",
                    content:"",
                    isUrgent:"",
                    channelBelong:"",
                },
                announcementInfoFormValidate:{
                    isUrgent: [
                        { required: true, message: "是否紧急不能为空", trigger: "change"}
                    ],
                    channelBelong: [
                        { required: true, message: "渠道归属不能为空", trigger: "change"}
                    ],
                    title: [
                        { required: true, message: "标题不能为空", trigger: "blur"},
                    ],
                    content: [
                        { required: true, message: "内容不能为空", trigger: "blur"},
                    ]
                },
                columns: [
                    {
                        type: "selection",
                        minWidth: 60,
                        maxWidth:60,
                        align: "center",
                        fixed: "left"
                    },
                    {
                        type: 'index',
                        title:"序号",
                        minWidth: 60,
                        fixed: "left",
                    },
                    {
                        title: "标题",
                        key: "title",
                        minWidth: 100,
                        align: "center",
                    },
                    {
                        title: "内容",
                        key: "content",
                        minWidth: 300,
                        align: "center",
                        ellipsis:true,
                        render: (h, params) => {
                            return h('div', [
                                h('span', {
                                    style: {
                                        display: 'inline-block',
                                        width: '100%',
                                        overflow: 'hidden',
                                        textOverflow: 'ellipsis',
                                        whiteSpace: 'nowrap'
                                    },
                                    domProps: {
                                        title: params.row.content
                                    }
                                }, params.row.content)
                            ])
                        }
                    },
                    {
                        title: "是否紧急",
                        key: "isUrgent",
                        minWidth: 60,
                        align: "center",
                        slot:"isUrgent"
                    },
                    {
                        title: "渠道归属",
                        key: "channelBelong",
                        minWidth: 60,
                        align: "center",
                        slot:"channelBelong"
                    },
                    {
                        title: "发布人员",
                        key: "createBy",
                        minWidth: 100,
                        align: "center",
                    },
                    {
                        title: "创建时间",
                        key: "createTime",
                        minWidth: 300,
                        align: "center",
                    },
                    {
                        title: "操作",
                        key: "action",
                        width: 80,
                        align: "center",
                        fixed: "right",
                        slot: "action"
                    }
                ],
                data: [],
                total: 0
            };
        },
        methods: {
            init() {
                this.getAnnouncementInfo();
                this.initLabelType();
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getAnnouncementInfo();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getAnnouncementInfo();
            },
            selectDateRange(v) {
                if (v) {
                    this.searchForm.startDate = v[0];
                    this.searchForm.endDate = v[1];
                }
            },
            getAnnouncementInfo() {
                // 多条件app公告列表
                this.loading = true;
                getAnnouncementInfo(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.code === "0000") {
                        this.data = res.data.records;
                        this.total = res.data.total;
                    }
                });
            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getAnnouncementInfo();
            },
            handleReset() {
                this.$refs.searchForm.resetFields();
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                // 重新加载数据
                this.getAnnouncementInfo();
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getAnnouncementInfo();
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "更多";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            setIsUrgentListTitle(val){
                if(val==null ||val=="" ||val ==undefined){
                    return;
                }
                let dic = {};
                this.isUrgentList.forEach(column => {
                    if(column.value === val){
                        dic = column;
                    }
                });
                return dic.title;
            },

            setChannelBelongTitle(val){
                if(val==null ||val=="" ||val ==undefined){
                    return;
                }
                let dic = {};
                this.channelBelongList.forEach(column => {
                    if(column.value === val){
                        dic = column;
                    }
                });
                return dic.title;
            },

            initLabelType(){
                let channelBelongDic="channelBelong";
                getDictDataByType(channelBelongDic).then(res =>{
                    if (res.code === "0000"){
                        this.channelBelongList = res.data;
                    }
                });

                let isUrgentListDic="isDefault";
                getDictDataByType(isUrgentListDic).then(res =>{
                    if (res.code === "0000"){
                        this.isUrgentList = res.data;
                    }
                });
            },
            add() {
                this.modalType = 0;
                this.modalTitle = "新增公告信息";
                this.$refs.announcementInfoForm.resetFields();
                this.announcementInfoForm.id=null;
                this.announcementInfoForm.createTime=null;
                this.announcementInfoModalVisible = true;
            },
            edit(v) {
                this.modalType = 1;
                this.modalTitle = "编辑公告信息";
                this.$refs.announcementInfoForm.resetFields();
                // 转换null为""
                for (let attr in v) {
                    if (v[attr] === null) {
                        v[attr] = "";
                    }
                }
                let str = JSON.stringify(v);
                let announcementInfo = JSON.parse(str);
                this.announcementInfoForm = announcementInfo;
                this.announcementInfoModalVisible = true;
            },
            save(){
                this.$refs.announcementInfoForm.validate(valid => {
                    if (valid) {
                        this.saveLoading = true;
                        saveAnnouncementInfo(this.announcementInfoForm).then(res => {
                            this.saveLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("保存成功");
                                this.getAnnouncementInfo();
                                this.announcementInfoModalVisible = false;
                            }
                        }).catch(() => {
                            this.saveLoading = false;
                        });
                    }

                });
            },
            delAll() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择要删除的数据");
                    return;
                }
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
                    onOk: () => {
                        let ids = [];
                        this.selectList.forEach(function (e) {
                            ids += e.id + ",";
                        });
                        ids = ids.substring(0, ids.length - 1);
                        this.operationLoading = true;
                        delAnnouncementInfo(ids).then(res => {
                            this.operationLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.clearSelectAll();
                                this.getAnnouncementInfo();
                            }
                        });
                    }
                });
            },
            cancel() {
                //关闭窗口
                this.announcementInfoModalVisible = false;
            },
        },
        created() {

        },
        mounted() {
            this.init();
        },
    };
</script>
<style scoped>
</style>
